﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace HFController
{
    /// <summary>
    /// 回复逻辑管理
    /// </summary>
    public class AnswerController:ControllerBase
    {
        private static AnswerController _answerController;
        public static AnswerController getController()
        {
            if (_answerController == null)
            {
                _answerController = new AnswerController();
            }
            return _answerController;
        }

        #region 回复

        /// <summary>
        /// 回复数据库访问
        /// </summary>
        private hf.DAL.Answer AnswerDAL = new hf.DAL.Answer();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="AskID"></param>
        /// <param name="Content"></param>
        /// <param name="SecretContent"></param>
        /// <returns></returns>
        public HFModel.ResultModel doAddAnswer(int AskID, string Content, string SecretContent)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Answer model = new hf.Model.Answer();
            BagePages bp = new BagePages();
            hf.Model.Lawyer lawyer = bp.getLoginLawyer();
            if (lawyer == null)
            {
                result.isTrue = false;
                result.Message = "请先登录！";
            }
            else
            {
                model.AskID = AskID;
                model.Addtime = DateTime.Now;
                model.Content = Content;
                model.LawyerID = lawyer.ID;
                model.SecretContent = SecretContent;
                try
                {
                    model.ID = AnswerDAL.Add(model);
                    hf.Model.Ask askmodel = AskController.getController().getAskModel(model.AskID);
                    askmodel.AnswerCount++;
                    new hf.DAL.Ask().Update(askmodel);
                    result.isTrue = true;
                    result.Message = "添加成功！";
                    result.goUrl = "location='Answer.aspx';";
                    result.historyUrl = "history.go(-1)";
                }
                catch(Exception err)
                {
                    result.isTrue = false;
                    result.Message = err.Message;
                    result.goUrl = "history.go(-1)";
                    result.historyUrl = "history.go(-1)";
                }
            }
            return result;
        }

        /// <summary>
        /// 添加回复
        /// </summary>
        /// <param name="AskID"></param>
        /// <param name="Addtime"></param>
        /// <param name="Content"></param>
        /// <param name="LawyerID"></param>
        /// <param name="SecretContent"></param>
        public void doAddAnswer(int AskID, string Content, int LawyerID, string SecretContent)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Answer model = new hf.Model.Answer();

            model.AskID = AskID;
            model.Addtime = DateTime.Now;
            model.Content = Content;
            model.LawyerID = LawyerID;
            model.SecretContent = SecretContent;

            try
            {
                model.ID = AnswerDAL.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='Answer.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 根据ID 得到一条回复
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.Answer getAnswerModel(int ID)
        {
            hf.Model.Answer model = new hf.Model.Answer();

            int total;
            List<hf.Model.Answer> list = AnswerDAL.GetAnswerList(1, 1, "ID=" + ID.ToString(), out total);
            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 修改回复
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="AskID"></param>
        /// <param name="Content"></param>
        /// <param name="LawyerID"></param>
        /// <param name="SecretContent"></param>
        public void doModifyAnswer(int ID, int AskID, int LawyerID, string Content, string SecretContent)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Answer model = this.getAnswerModel(ID);

            model.AskID = AskID;
            model.Content = Content;
            model.LawyerID = LawyerID;
            model.SecretContent = SecretContent;

            try
            {
                //执行修改
                AnswerDAL.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='AnswerList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 得到问答的问题ID数组
        /// </summary>
        /// <param name="LawyerID"></param>
        /// <returns></returns>
        public string getAnswerAskIDList(int LawyerID)
        {
            return AnswerDAL.GetAnswerAskIDList(LawyerID);
        }

        /// <summary>
        /// 得到列表，前台用
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageNumber"></param>
        /// <param name="strWhere"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<hf.Model.Answer> getAnswerList(int PageSize ,int PageNumber ,string strWhere ,out int total)
        {
            List<hf.Model.Answer> list = new List<hf.Model.Answer>();

            //获得列表
            list = AnswerDAL.GetAnswerList(PageSize, PageNumber, strWhere, out total);
            this.bindList(list);
            return list;
        }

        private void bindList(List<hf.Model.Answer> list)
        { 
            foreach(hf.Model.Answer a in list)
            {
                hf.Model.Lawyer lawyer = LawyerController.getController().getLawyerModel(a.LawyerID);
                if (lawyer != null)
                    a.lawyer = lawyer;
            }
        }

        /// <summary>
        /// 根据律师ID得到回复信息
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="LayerID"></param>
        /// <returns></returns>
        public List<hf.Model.Answer> getAnswerList(int Count, int LayerID)
        {
            List<hf.Model.Answer> list = new List<hf.Model.Answer>();

            //每页显示的记录
            int PageSize = Count;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }

            //总数据记录
            int total;

            //筛选条件
            string strSql = "LawyerID=" + LayerID.ToString();

            //获得列表
            list = AnswerDAL.GetAnswerList(PageSize, PageNumber, strSql, out total);

            return list;
        }

        /// <summary>
        /// 得到回答列表
        /// </summary>
        /// <param name="Count"></param>
        /// <returns></returns>
        public List<hf.Model.Answer> getAnswerList(int Count)
        {
            List<hf.Model.Answer> list = new List<hf.Model.Answer>();

            //每页显示的记录
            int PageSize = Count;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }

            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = AnswerDAL.GetAnswerList(PageSize, PageNumber, strSql, out total);

            return list;
        }

        /// <summary>
        /// 得到答复列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.Answer> getAnswerList(out string PagerStr)
        {
            List<hf.Model.Answer> list = new List<hf.Model.Answer>();

            //每页显示的记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }

            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = AnswerDAL.GetAnswerList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 删除答复
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeAnswer(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
                AnswerDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='AnswerList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="IDS"></param>
        public void doDelegeAnswer(string[] IDS)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                for (int i = 0; i < IDS.Length; i++)
                {
                    AnswerDAL.Delete(int.Parse(IDS[i].Trim()));
                }
                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='AnswerList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        } 
        #endregion

        /// <summary>
        /// 律师留言数据库访问
        /// </summary>
        private hf.DAL.LawyerNote LawyerNoteDAL = new hf.DAL.LawyerNote();

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.LawyerNote> getLawyerNoteList(out string PagerStr)
        {
            List<hf.Model.LawyerNote> list = new List<hf.Model.LawyerNote>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = LawyerNoteDAL.GetLawyerNoteList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 删除留言
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeLawyerNote(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
                LawyerNoteDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='LawyerNoteList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="IDS"></param>
        public void doDelegeLawyerNote(string[] IDS)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                for (int i = 0; i < IDS.Length; i++)
                {
                    LawyerNoteDAL.Delete(int.Parse(IDS[i].Trim()));
                }
                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='LawyerNoteList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
    }
}
